package com.apes.scm.masterdata.party.model;

import com.alibaba.fastjson.annotation.JSONField;
import com.apes.framework.api.SpringManager;
import com.apes.framework.jpa.ApesBean;
import com.apes.framework.jpa.BaseModel;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Name;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.jpa.generator.ApesGenerators;
import com.apes.framework.util.JpaUtil;
import com.apes.scm.masterdata.party.repository.AccessProviderRepository;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;
import org.springframework.util.StringUtils;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 功能：当事人角色
 *
 * @author xul
 * @create 2018-01-24 13:37
 */
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = PartyRole.CONST_TABLE_NAME, indexes = {
        @Index(name = "idx_mas_party_role", columnList = "PARTY_ID,ROLE_ID", unique = true),
        @Index(name = "idx_mas_party_role_party_id", columnList = "PARTY_ID"),
        @Index(name = "idx_mas_party_role_role_id", columnList = "ROLE_ID")})
@ApesGenerators({@ApesGenerator(code = "PARTY_Company_Direct_Store", name = "直营公司编码", padding = 4),
        @ApesGenerator(code = "PARTY_Company_Partner", name = "合伙公司编码", padding = 4,numberNext = 501),
        @ApesGenerator(code = "PARTY_Company_Empower", name = "赋能公司编码", padding = 4,numberNext = 6001),
        @ApesGenerator(code = "PARTY_Customer", name = "客户编码", padding = 8),
        @ApesGenerator(code = "PARTY_Supplier", name = "供应商编码", padding = 6),
        @ApesGenerator(code = "PARTY_Operator", name = "运营商编码", padding = 9),
        @ApesGenerator(code = "PARTY_Employee", name = "员工编码", padding = 7)})
public class PartyRole extends BaseModel {
    public static final String CONST_TABLE_NAME = "mas_party_role";

    @Id
    @Column(name = "id", length = 32)
    private String id;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "PARTY_ID", referencedColumnName = "ID", nullable = false)
    private Party party; //当事人

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "ROLE_ID", referencedColumnName = "ID", nullable = false)
    private RoleType role; //角色类型

    @Comment("接入商")
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "access_provider_id", referencedColumnName = "id")
    private AccessProvider accessProvider; //接入商

    @Name
    @Column(name = "fin", columnDefinition = "number(1,0) default 0")
    @Comment("开启财务帐")
    private boolean fin = false;

    @Name
    @Transient
    private String name;
    public String getName() {
        if(!StringUtils.isEmpty(name)) return name;
        if(this.getParty() == null) return null;
        return this.getParty().getName();
    }

    /***
     * 功能：获取手机号
     * @return
     */
    public String queryMobileNumber() {
        if( party == null ) return null;
        return party.queryMobileNumber();
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != JpaUtil.getDomainClass((ApesBean) o)) return false;
        PartyRole partyRole = (PartyRole) o;
        return Objects.equals(id, partyRole.getId());
    }

    /***
     * 获取公司对应的接入商
     */
    public AccessProvider queryAccessProvider() {
        if( !(this.role != null && this.role.getId().equals("Company"))) return null;
        return SpringManager.getBean(AccessProviderRepository.class).findByCompany(this.getId());
    }

    @JSONField
    @OneToMany(mappedBy = "client" , fetch = FetchType.LAZY)
    private List<CustomerBusiness> customerBusinesses = new ArrayList<>(); //明细

}
